package com.sghd.football.module.game.service.core.task;

import com.sghd.football.module.game.service.GameService;
import com.sghd.football.module.game.service.core.Desk;
import com.sghd.football.module.game.service.core.Player;
import com.sghd.football.module.game.service.thread.Task;
import com.sghd.football.module.game.service.thread.TaskItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * @author wch
 * @date 2018-06-05 16:25
 **/
public class CleanTask implements Task {

    public Logger logger = LoggerFactory.getLogger(getClass());
    private Player player;
    private int timeout;
    private GameService gameService;

    public CleanTask(Player player, int timeout, GameService gameService) {
        this.player = player;
        this.timeout = timeout;
        this.gameService = gameService;
    }

    @Override
    public void run(Desk desk, Date now) {
        Date last = player.getLastUpdate();
        if ((now.getTime() - last.getTime()) >= timeout) {
            gameService.exit(player.getPlayerId(), true);
        } else {
            //下一次超时检测(循环任务)
            gameService.addTimer(desk, this::run, new Date(last.getTime() + timeout), TaskItem.MATCH);
        }
    }
}
